import { getBookList, getBookDetail } from '../services/book'

export default {

  namespace: 'book',

  state: {
    bookList: [],
    detail: {}
  },

  subscriptions: {
    setup({ dispatch, history }) {
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const res = yield call(getBookList, payload);
      if (res.data.code == 0) {
        yield put({ type: 'save', payload: res.data.data.records });
      }
    },
    *fetchDetail({ payload }, { call, put }) {
      const res = yield call(getBookDetail, payload);
      if (res.data.code == 0) {
        yield put({ type: 'saveDetail', payload: res.data.data });
      }
    },
  },

  reducers: {
    save(state, action) {
      return { ...state, bookList: action.payload };
    },
    saveDetail(state, action) {
      return { ...state, detail: action.payload };
    },
  },

};
